﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HalconDotNet;
using Project1.Element;
using Project1;
namespace Project1.Common
{
    public class GeoMethod
    {
        private readonly static HTuple hv_defaultOffsetX = 20;
        private readonly  static  HTuple hv_defaultOffsetY = 20;
        private readonly static HTuple Pi = new HTuple(0).TupleAcos() * 2;
        public GeoMethod()
        {

        }
        public static void LL_Cross(HObject ho_Line1Contours, HObject ho_Line2Contours,out HTuple hv_Row, out HTuple hv_Column)
        {
            // Local control variables 
            HTuple hv_RowBegin1 = null, hv_ColBegin1 = null;
            HTuple hv_RowEnd1 = null, hv_ColEnd1 = null, hv_Nr = null;
            HTuple hv_Nc = null, hv_Dist = null, hv_RowBegin2 = null;
            HTuple hv_ColBegin2 = null, hv_RowEnd2 = null, hv_ColEnd2 = null;
            HTuple hv_IsOverlapping = null;



            HOperatorSet.FitLineContourXld(ho_Line1Contours, "tukey", 20, 0, 10, 2, out hv_RowBegin1,
                out hv_ColBegin1, out hv_RowEnd1, out hv_ColEnd1, out hv_Nr, out hv_Nc, out hv_Dist);
            HOperatorSet.FitLineContourXld(ho_Line2Contours, "tukey", 20, 0, 10, 2, out hv_RowBegin2,
                out hv_ColBegin2, out hv_RowEnd2, out hv_ColEnd2, out hv_Nr, out hv_Nc, out hv_Dist);
            HOperatorSet.IntersectionLines(hv_RowBegin1, hv_ColBegin1, hv_RowEnd1, hv_ColEnd1,
                hv_RowBegin2, hv_ColBegin2, hv_RowEnd2, hv_ColEnd2, out hv_Row, out hv_Column,
                out hv_IsOverlapping);
            HOperatorSet.SetColor(Utils.hv_ExpDefaultWinHandle, "yellow");
            HOperatorSet.DispCircle(Utils.hv_ExpDefaultWinHandle, hv_Row, hv_Column, 5);

            return;
        }

        public static void LC_Cross(HObject ho_Line1Contours, HObject ho_CircleContours, out HTuple hv_Row, out HTuple hv_Column)
        {



            // Local control variables 

            HTuple hv_RowBegin = null, hv_ColBegin = null;
            HTuple hv_RowEnd = null, hv_ColEnd = null, hv_Nr = null;
            HTuple hv_Nc = null, hv_Dist = null, hv_IsOverlapping = null;
            // Initialize local and output iconic variables 

            //gen_contour_region_xld (CircleContours, CircleContours, 'border')
            HOperatorSet.FitLineContourXld(ho_Line1Contours, "tukey", 20, 0, 5, 2, out hv_RowBegin,
                out hv_ColBegin, out hv_RowEnd, out hv_ColEnd, out hv_Nr, out hv_Nc, out hv_Dist);
            HOperatorSet.IntersectionLineContourXld(ho_CircleContours, hv_RowBegin, hv_ColBegin,
                hv_RowEnd, hv_ColEnd, out hv_Row, out hv_Column, out hv_IsOverlapping);

            return;
        }

        public static void CC_Cross(HObject ho_Circle1Contours, HObject ho_Circle2Contours, out HTuple hv_Row, out HTuple hv_Column)
        {



            // Local control variables 

            HTuple hv_CRow1 = null, hv_CCol1 = null, hv_CRadius1 = null;
            HTuple hv_CStartPhi1 = null, hv_CEndPhi1 = null, hv_CPointOrder1 = null;
            HTuple hv_CRow2 = null, hv_CCol2 = null, hv_CRadius2 = null;
            HTuple hv_CStartPhi2 = null, hv_CEndPhi2 = null, hv_CPointOrder2 = null;
            HTuple hv_IsOverlapping1 = null;
            // Initialize local and output iconic variables 

            HOperatorSet.FitCircleContourXld(ho_Circle1Contours, "algebraic", -1, 0, 0, 3,
                2, out hv_CRow1, out hv_CCol1, out hv_CRadius1, out hv_CStartPhi1, out hv_CEndPhi1,
                out hv_CPointOrder1);
            HOperatorSet.FitCircleContourXld(ho_Circle2Contours, "algebraic", -1, 0, 0, 3,
                2, out hv_CRow2, out hv_CCol2, out hv_CRadius2, out hv_CStartPhi2, out hv_CEndPhi2,
                out hv_CPointOrder2);
            HOperatorSet.IntersectionCircles(hv_CRow1, hv_CCol1, hv_CRadius1, hv_CStartPhi1,
                hv_CEndPhi1, hv_CPointOrder1, hv_CRow2, hv_CCol2, hv_CRadius2, hv_CStartPhi2,
                hv_CEndPhi2, hv_CPointOrder2, out hv_Row, out hv_Column, out hv_IsOverlapping1);

            return;
        }

        public static void C_Center(HObject ho_Circle1, out HTuple hv_Row, out HTuple hv_Column)
        {



            // Local iconic variables 

            HObject ho_Circle1Contours;

            // Local control variables 

            HTuple hv_Radius = null, hv_StartPhi = null;
            HTuple hv_EndPhi = null, hv_PointOrder = null;
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Circle1Contours);
            ho_Circle1Contours.Dispose();
            //HOperatorSet.GenContourRegionXld(ho_Circle1, out ho_Circle1Contours, "border");
            HOperatorSet.FitCircleContourXld(ho_Circle1, "algebraic", -1, 0, 0, 3,
                2, out hv_Row, out hv_Column, out hv_Radius, out hv_StartPhi, out hv_EndPhi,
                out hv_PointOrder);
            ho_Circle1Contours.Dispose();

            return;
        }

        public static void P_Mid(HTuple hv_Row1, HTuple hv_Col1, HTuple hv_Row2, HTuple hv_Col2, out HTuple hv_Row, out HTuple hv_Col)
        {
            // Local iconic variables 
            // Initialize local and output iconic variables 
            hv_Row = (0.5 * (hv_Row1.TupleReal())) + (0.5 * (hv_Row2.TupleReal()));
            hv_Col = (0.5 * (hv_Col1.TupleReal())) + (0.5 * (hv_Col2.TupleReal()));

            return;
        }

        public static void LL_Mid(HObject ho_Line1Contours, HObject ho_Line2Contours, out HObject ho_MidLine, out HTuple hv_MidRowBegin, out HTuple hv_MidColBegin, out HTuple hv_MidRowEnd, out HTuple hv_MidColEnd)
        {
            hv_MidColEnd = null;
            hv_MidRowEnd = null;
            hv_MidColBegin = null;
            hv_MidRowBegin = null;
            HTuple hv_RowBegin1 = null, hv_ColBegin1 = null;
            HTuple hv_RowEnd1 = null, hv_ColEnd1 = null, hv_Nr = null;
            HTuple hv_Nc = null, hv_Dist = null, hv_RowBegin2 = null;
            HTuple hv_ColBegin2 = null, hv_RowEnd2 = null, hv_ColEnd2 = null;
            HTuple hv_Row = null, hv_Column = null, hv_IsOverlapping = null;
            HTuple hv_theta1 = null, hv_theta2 = null, hv_MidRow = null;
            HTuple hv_MidCol = null;
            HTuple hv_midTheta = null;
            HTuple hv_Col = new HTuple(), hv_k = new HTuple(), hv_midTan = new HTuple();
          
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_MidLine);
          
            //skeleton (Line1Contours, Line1Contours)
            //skeleton (Line2Contours, Line2Contours)
            //gen_contours_skeleton_xld (Line1Contours, Line1Contours, 1, 'filter')
            //gen_contours_skeleton_xld (Line2Contours, Line2Contours, 1, 'filter')
            HOperatorSet.FitLineContourXld(ho_Line1Contours, "tukey", 20, 0, 10, 2, out hv_RowBegin1,
                out hv_ColBegin1, out hv_RowEnd1, out hv_ColEnd1, out hv_Nr, out hv_Nc, out hv_Dist);
            HOperatorSet.FitLineContourXld(ho_Line2Contours, "tukey", 20, 0, 10, 2, out hv_RowBegin2,
                out hv_ColBegin2, out hv_RowEnd2, out hv_ColEnd2, out hv_Nr, out hv_Nc, out hv_Dist);
            HOperatorSet.IntersectionLines(hv_RowBegin1, hv_ColBegin1, hv_RowEnd1, hv_ColEnd1,
                hv_RowBegin2, hv_ColBegin2, hv_RowEnd2, hv_ColEnd2, out hv_Row, out hv_Column,
                out hv_IsOverlapping);

            HOperatorSet.LineOrientation(hv_RowBegin1, hv_ColBegin1, hv_RowEnd1, hv_ColEnd1,
                out hv_theta1);
            HOperatorSet.LineOrientation(hv_RowBegin2, hv_ColBegin2, hv_RowEnd2, hv_ColEnd2,
                out hv_theta2);
            if ((int)(new HTuple(hv_theta1.TupleLess(0))) != 0)
            {
                hv_theta1 = hv_theta1 + Pi;
            }
            if ((int)(new HTuple(hv_theta2.TupleLess(0))) != 0)
            {
                hv_theta2 = hv_theta2 + Pi;
            }

            hv_MidRow = 0;
            hv_MidCol = 0;
            hv_MidColEnd = 0;
            hv_MidRowEnd = 0;
            hv_MidColBegin = 0;
            hv_MidRowBegin = 0;

            hv_midTheta = (0.5 * hv_theta1) + (0.5 * hv_theta2);
            hv_MidRow = (0.5 * hv_RowBegin1) + (0.5 * hv_RowBegin2);
            hv_MidCol = (0.5 * hv_ColBegin1) + (0.5 * hv_ColBegin2);

            if ((int)(new HTuple(((((hv_theta1 - hv_theta2)).TupleAbs())).TupleLessEqual(0.5))) != 0)
            {

                if ((int)((new HTuple(hv_midTheta.TupleEqual(0.5 * Pi))).TupleOr(new HTuple(hv_midTheta.TupleEqual(
                    1.5 * Pi)))) != 0)
                {
                    hv_MidColEnd = hv_MidCol.Clone();
                    hv_MidRowEnd = hv_MidRow + hv_defaultOffsetY;
                    hv_MidColBegin = hv_MidCol.Clone();
                    hv_MidRowBegin = hv_MidRow - hv_defaultOffsetY;
                    hv_Row = new HTuple();
                    hv_Row = hv_Row.TupleConcat(hv_MidRowBegin);
                    hv_Row = hv_Row.TupleConcat(hv_MidRowEnd);
                    hv_Col = new HTuple();
                    hv_Col = hv_Col.TupleConcat(hv_MidColBegin);
                    hv_Col = hv_Col.TupleConcat(hv_MidColEnd);
                }
                else
                {
                    hv_k = -1 * (hv_midTheta.TupleTan());
                    hv_MidColEnd = hv_MidCol + hv_defaultOffsetX;
                    hv_MidRowEnd = hv_MidRow + (hv_defaultOffsetX * hv_k);
                    hv_MidColBegin = hv_MidCol - hv_defaultOffsetX;
                    hv_MidRowBegin = hv_MidRow - (hv_defaultOffsetX * hv_k);
                    hv_Row = new HTuple();
                    hv_Row = hv_Row.TupleConcat(hv_MidRowBegin);
                    hv_Row = hv_Row.TupleConcat(hv_MidRowEnd);
                    hv_Col = new HTuple();
                    hv_Col = hv_Col.TupleConcat(hv_MidColBegin);
                    hv_Col = hv_Col.TupleConcat(hv_MidColEnd);
                }

            }
            else
            {

                if ((int)((new HTuple(hv_midTheta.TupleEqual(0.5 * Pi))).TupleOr(new HTuple(hv_midTheta.TupleEqual( 1.5 * Pi)))) != 0)
                {
                    hv_MidColEnd = hv_MidCol.Clone();
                    hv_MidRowEnd = hv_MidRow + hv_defaultOffsetY;
                    hv_MidColBegin = hv_MidCol.Clone();
                    hv_MidRowBegin = hv_MidRow - hv_defaultOffsetY;
                    hv_Row = new HTuple();
                    hv_Row = hv_Row.TupleConcat(hv_MidRowBegin);
                    hv_Row = hv_Row.TupleConcat(hv_MidRowEnd);
                    hv_Col = new HTuple();
                    hv_Col = hv_Col.TupleConcat(hv_MidColBegin);
                    hv_Col = hv_Col.TupleConcat(hv_MidColEnd);
                }
                else
                {
                    hv_midTan = hv_midTheta.TupleTan();
                    hv_k = -hv_midTan;
                    hv_MidColEnd = hv_MidCol + hv_defaultOffsetX;
                    hv_MidRowEnd = hv_MidRow + (hv_defaultOffsetX * hv_k);
                    hv_MidColBegin = hv_MidCol - hv_defaultOffsetX;
                    hv_MidRowBegin = hv_MidRow - (hv_defaultOffsetX * hv_k);
                    hv_Row = new HTuple();
                    hv_Row = hv_Row.TupleConcat(hv_MidRowBegin);
                    hv_Row = hv_Row.TupleConcat(hv_MidRowEnd);
                    hv_Col = new HTuple();
                    hv_Col = hv_Col.TupleConcat(hv_MidColBegin);
                    hv_Col = hv_Col.TupleConcat(hv_MidColEnd);
                }

            }
            ho_MidLine.Dispose();
            HOperatorSet.GenContourPolygonXld(out ho_MidLine, hv_Row, hv_Col);
            //HOperatorSet.DispLine(Utils.hv_ExpDefaultWinHandle, hv_MidRowBegin, hv_MidColBegin, hv_MidRowEnd, hv_MidColEnd);
            return;
        }

        public static void PL_Mid(HObject ho_Line, out HObject ho_MidLine, HTuple hv_Row, HTuple hv_Column, out HTuple hv_MidRowBegin, out HTuple hv_MidColBegin, out HTuple hv_MidRowEnd, out HTuple hv_MidColEnd)
        {
            // Local iconic variables 
            HObject ho_VerticalLine;

            // Local control variables 

            HTuple Pi = null;
            HTuple hv_RowBegin = null, hv_ColBegin = null, hv_RowEnd = null;
            HTuple hv_ColEnd = null, hv_Nr1 = null, hv_Nc1 = null;
            HTuple hv_Dist1 = null, hv_Distance = null, hv_Rows = null;
            HTuple hv_Cols = null, hv_aimRow = null, hv_aimCol = null;
            HTuple hv_MidRow = null, hv_MidCol = null, hv_minDist = null;
            HTuple hv_minIndex = null, hv_i = null, hv_PPDist = new HTuple();
            HTuple hv_Phi = null, hv_k = null;
            HTuple hv_Col = null;
            HTuple hv_Row_COPY_INP_TMP = hv_Row.Clone();
            hv_MidColEnd = null;
            hv_MidRowEnd = null;
            hv_MidColBegin = null;
            hv_MidRowBegin = null;
            
           
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_MidLine);
            HOperatorSet.GenEmptyObj(out ho_VerticalLine);
            Pi = ((new HTuple(0)).TupleAcos()) * 2;
            HOperatorSet.FitLineContourXld(ho_Line, "tukey", 20, 0, 10, 2, out hv_RowBegin,
                out hv_ColBegin, out hv_RowEnd, out hv_ColEnd, out hv_Nr1, out hv_Nc1, out hv_Dist1);
            HOperatorSet.DistancePl(hv_Row_COPY_INP_TMP, hv_Column, hv_RowBegin, hv_ColBegin,
                hv_RowEnd, hv_ColEnd, out hv_Distance);
            HOperatorSet.GetContourXld(ho_Line, out hv_Rows, out hv_Cols);
            hv_aimRow = 0;
            hv_aimCol = 0;
            hv_MidRow = 0;
            hv_MidCol = 0;
            ho_VerticalLine.Dispose();
            HOperatorSet.GenEmptyObj(out ho_VerticalLine);
            hv_minDist = 9999;
            hv_minIndex = -1;
            for (hv_i = 0; (int)hv_i <= (int)((new HTuple(hv_Rows.TupleLength())) - 1); hv_i = (int)hv_i + 1)
            {
                HOperatorSet.DistancePp(hv_Row_COPY_INP_TMP, hv_Column, hv_Rows.TupleSelect(
                    hv_i), hv_Cols.TupleSelect(hv_i), out hv_PPDist);

                if ((int)(new HTuple(hv_minDist.TupleGreater(hv_PPDist))) != 0)
                {
                    hv_minDist = hv_PPDist.Clone();
                    hv_minIndex = hv_i.Clone();
                }

            }

            if ((int)(new HTuple(((((hv_minDist - hv_Distance)).TupleAbs())).TupleLessEqual(
                0.01 * hv_Distance))) != 0)
            {
                hv_aimRow = hv_Rows.TupleSelect(hv_minIndex);
                hv_aimCol = hv_Cols.TupleSelect(hv_minIndex);
                ho_VerticalLine.Dispose();
                HOperatorSet.GenContourPolygonXld(out ho_VerticalLine, hv_aimRow.TupleConcat(
                    hv_Row_COPY_INP_TMP), hv_aimCol.TupleConcat(hv_Column));
            }
            else
            {
                ho_VerticalLine.Dispose();

                return;
            }
            hv_MidRow = (0.5 * hv_aimRow) + (0.5 * hv_Row_COPY_INP_TMP);
            hv_MidCol = (0.5 * hv_aimCol) + (0.5 * hv_Column);
            HOperatorSet.LineOrientation(hv_Row_COPY_INP_TMP, hv_Column, hv_aimRow, hv_aimCol,
                out hv_Phi);
            hv_k = (-1 * 1) / (hv_Phi.TupleTan());
            hv_k = -1 * hv_k;
            hv_MidColEnd = hv_MidCol + hv_defaultOffsetX;
            hv_MidRowEnd = hv_MidRow + (hv_defaultOffsetX * hv_k);
            hv_MidColBegin = hv_MidCol - hv_defaultOffsetX;
            hv_MidRowBegin = hv_MidRow - (hv_defaultOffsetX * hv_k);
            hv_Row_COPY_INP_TMP = new HTuple();
            hv_Row_COPY_INP_TMP = hv_Row_COPY_INP_TMP.TupleConcat(hv_MidRowBegin);
            hv_Row_COPY_INP_TMP = hv_Row_COPY_INP_TMP.TupleConcat(hv_MidRowEnd);
            hv_Col = new HTuple();
            hv_Col = hv_Col.TupleConcat(hv_MidColBegin);
            hv_Col = hv_Col.TupleConcat(hv_MidColEnd);
            ho_MidLine.Dispose();
            HOperatorSet.GenContourPolygonXld(out ho_MidLine, hv_Row_COPY_INP_TMP, hv_Col);
            //HOperatorSet.DispLine(Utils.hv_ExpDefaultWinHandle, hv_MidRowBegin, hv_MidColBegin,hv_MidRowEnd, hv_MidColEnd);
           
            ho_VerticalLine.Dispose();
            return;
        }

        public static void PL_VERTICAL(HObject ho_Line, out HObject ho_VerticalLine, HTuple hv_Row, HTuple hv_Column, out HTuple hv_MidRowBegin, out HTuple hv_MidColBegin, out HTuple hv_MidRowEnd, out HTuple hv_MidColEnd)
        {
            hv_MidColEnd = null;
            hv_MidRowEnd = null;
            hv_MidColBegin = null;
            hv_MidRowBegin = null;
            // Local iconic variables 

            // Local control variables 

            HTuple hv_RowBegin = null, hv_ColBegin = null;
            HTuple hv_RowEnd = null, hv_ColEnd = null, hv_Nr1 = null;
            HTuple hv_Nc1 = null, hv_Dist1 = null, hv_Distance = null;
            HTuple hv_Rows = null, hv_Cols = null, hv_aimRow = null;
            HTuple hv_aimCol = null, hv_minDist = null, hv_minIndex = null;
            HTuple hv_i = null, hv_PPDist = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_VerticalLine);
            HOperatorSet.FitLineContourXld(ho_Line, "tukey", 20, 0, 10, 2, out hv_RowBegin,out hv_ColBegin, out hv_RowEnd, out hv_ColEnd, out hv_Nr1, out hv_Nc1, out hv_Dist1);
            HOperatorSet.DistancePl(hv_Row, hv_Column, hv_RowBegin, hv_ColBegin, hv_RowEnd, hv_ColEnd, out hv_Distance);
            HOperatorSet.GetContourXld(ho_Line, out hv_Rows, out hv_Cols);
            hv_aimRow = 0;
            hv_aimCol = 0;
            hv_minDist = 9999;
            hv_minIndex = -1;
            for (hv_i = 0; (int)hv_i <= (int)((new HTuple(hv_Rows.TupleLength())) - 1); hv_i = (int)hv_i + 1)
            {
                HOperatorSet.DistancePp(hv_Row, hv_Column, hv_Rows.TupleSelect(hv_i), hv_Cols.TupleSelect(
                    hv_i), out hv_PPDist);

                if ((int)(new HTuple(hv_minDist.TupleGreater(hv_PPDist))) != 0)
                {
                    hv_minDist = hv_PPDist.Clone();
                    hv_minIndex = hv_i.Clone();
                }

            }

            if ((int)(new HTuple(((((hv_minDist - hv_Distance)).TupleAbs())).TupleLessEqual( 0.01 * hv_Distance))) != 0)
            {
                hv_aimRow = hv_Rows.TupleSelect(hv_minIndex);
                hv_aimCol = hv_Cols.TupleSelect(hv_minIndex);
                ho_VerticalLine.Dispose();
                HOperatorSet.GenContourPolygonXld(out ho_VerticalLine, hv_aimRow.TupleConcat(hv_Row), hv_aimCol.TupleConcat(hv_Column));
            }

              
                hv_MidRowBegin = hv_aimRow;
                hv_MidColBegin = hv_aimCol;
                hv_MidColEnd = hv_Column;
                hv_MidRowEnd = hv_Row;
             
                return;
        }

        public static void PL_Parallel(HObject ho_LineContours, HTuple hv_Row, HTuple hv_Column, out HObject ho_ParalleLine, out HTuple hv_RowBegin, out HTuple hv_ColBegin, out HTuple hv_RowEnd, out HTuple hv_ColEnd)
        {

            hv_RowBegin = null;
            hv_ColBegin = null;
            hv_RowEnd = null;
            hv_ColEnd = null;




            // Local control variables 

       
            HTuple Pi = null;
            HTuple hv_Nr1 = null;
            HTuple hv_Nc1 = null, hv_Dist1 = null, hv_Phi = null, hv_k = new HTuple();
            HTuple hv_Col = null;
            HTuple hv_Row_COPY_INP_TMP = hv_Row.Clone();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_ParalleLine);
            Pi = ((new HTuple(0)).TupleAcos()) * 2;
            //skeleton (LineContours, LineContours)
            //gen_contours_skeleton_xld (LineContours, LineContours, 1, 'filter')
            HOperatorSet.FitLineContourXld(ho_LineContours, "tukey", 20, 0, 10, 2, out hv_RowBegin, out hv_ColBegin, out hv_RowEnd, out hv_ColEnd, out hv_Nr1, out hv_Nc1, out hv_Dist1);
            HOperatorSet.LineOrientation(hv_RowBegin, hv_ColBegin, hv_RowEnd, hv_ColEnd, out hv_Phi);
            if ((int)((new HTuple((hv_Phi.TupleAbs()).TupleEqual(0.5 * Pi))).TupleOr(new HTuple(((hv_Phi.TupleAbs()  )).TupleEqual(1.5 * Pi)))) != 0)
            {
                hv_ColEnd = hv_Column.Clone();
                hv_RowEnd = hv_Row_COPY_INP_TMP + hv_defaultOffsetY;
                hv_ColBegin = hv_Column.Clone();
                hv_RowBegin = hv_Row_COPY_INP_TMP - hv_defaultOffsetY;
            }
            else
            {
                hv_k = -1 * (hv_Phi.TupleTan());
                hv_ColEnd = hv_Column + hv_defaultOffsetX;
                hv_RowEnd = hv_Row_COPY_INP_TMP + (hv_defaultOffsetX * hv_k);
                hv_ColBegin = hv_Column - hv_defaultOffsetX;
                hv_RowBegin = hv_Row_COPY_INP_TMP - (hv_defaultOffsetX * hv_k);

            }
            hv_Row_COPY_INP_TMP = new HTuple();
            hv_Row_COPY_INP_TMP = hv_Row_COPY_INP_TMP.TupleConcat(hv_RowBegin);
            hv_Row_COPY_INP_TMP = hv_Row_COPY_INP_TMP.TupleConcat(hv_RowEnd);
            hv_Col = new HTuple();
            hv_Col = hv_Col.TupleConcat(hv_ColBegin);
            hv_Col = hv_Col.TupleConcat(hv_ColEnd);
            ho_ParalleLine.Dispose();
            HOperatorSet.GenContourPolygonXld(out ho_ParalleLine, hv_Row_COPY_INP_TMP, hv_Col);
            ho_ParalleLine.Dispose();
            return;
        }

        public static void PP_Mid(out HObject ho_Line, HTuple hv_Row1, HTuple hv_Col1, HTuple hv_Row2,HTuple hv_Col2, out HTuple hv_RowBegin, out HTuple hv_ColBegin, out HTuple hv_RowEnd, out HTuple hv_ColEnd)
        {

            // Local iconic variables 

            HObject ho_Contour = null;

            // Local control variables 

            HTuple  hv_MidRow = null, hv_MidCol = null;
            HTuple hv_Phi = null;
            HTuple hv_k = new HTuple(), hv_Row = null, hv_Col = null;

            hv_ColEnd = null;
            hv_RowEnd = null;
            hv_ColBegin = null;
            hv_RowBegin = null; ;
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Line);
            ho_Contour.Dispose();
            HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_Row1.TupleConcat(hv_Row2),
                hv_Col1.TupleConcat(hv_Col2));
            hv_MidRow = (0.5 * hv_Row1) + (0.5 * hv_Row2);
            hv_MidCol = (0.5 * hv_Col1) + (0.5 * hv_Col2);
            HOperatorSet.LineOrientation(hv_Row1, hv_Col1, hv_Row2, hv_Col2, out hv_Phi);


            if ((int)((new HTuple(((hv_Phi.TupleAbs())).TupleEqual(0.5 * Pi))).TupleOr(new HTuple(((hv_Phi.TupleAbs())).TupleEqual(1.5 * Pi)))) != 0)
            {
                hv_ColEnd = hv_MidCol.Clone();
                hv_RowEnd = hv_MidRow + hv_defaultOffsetY;
                hv_ColBegin = hv_MidCol.Clone();
                hv_RowBegin = hv_MidRow - hv_defaultOffsetY;
            }
            else
            {
                hv_k = (-1 * 1) / (hv_Phi.TupleTan());
                hv_k = hv_k * -1;
                hv_ColEnd = hv_MidCol + hv_defaultOffsetX;
                hv_RowEnd = hv_MidRow + (hv_defaultOffsetX * hv_k);
                hv_ColBegin = hv_MidCol - hv_defaultOffsetX;
                hv_RowBegin = hv_MidRow - (hv_defaultOffsetX * hv_k);

            }
            hv_Row = new HTuple();
            hv_Row = hv_Row.TupleConcat(hv_RowBegin);
            hv_Row = hv_Row.TupleConcat(hv_RowEnd);
            hv_Col = new HTuple();
            hv_Col = hv_Col.TupleConcat(hv_ColBegin);
            hv_Col = hv_Col.TupleConcat(hv_ColEnd);
            ho_Line.Dispose();
            HOperatorSet.GenContourPolygonXld(out ho_Line, hv_Row, hv_Col);

            ho_Contour.Dispose();

            return;
        }

        public static void P_FitLine(out HObject ho_FitLine, HTuple hv_Rows, HTuple hv_Cols, out HTuple hv_RowBegin, out HTuple hv_ColBegin, out HTuple hv_RowEnd, out HTuple hv_ColEnd)
        {



            // Local iconic variables 

            HObject ho_Contour;

            // Local control variables 

            hv_RowBegin = null;
            hv_ColBegin = null;
            hv_RowEnd = null;
            hv_ColEnd = null;

            HTuple hv_Nr = null;
            HTuple hv_Nc = null, hv_Dist = null;

            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_FitLine);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            ho_Contour.Dispose();
            HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_Rows, hv_Cols);
            HOperatorSet.FitLineContourXld(ho_Contour, "tukey", -1, 0, 5, 2, out hv_RowBegin,out hv_ColBegin, out hv_RowEnd, out hv_ColEnd, out hv_Nr, out hv_Nc, out hv_Dist);
            ho_FitLine.Dispose();
            HOperatorSet.GenContourPolygonXld(out ho_FitLine, hv_RowBegin.TupleConcat(hv_RowEnd), hv_ColBegin.TupleConcat(hv_ColEnd));
            ho_Contour.Dispose();

            return;
        }

        public static void LL_Concate(HObject ho_LineContours,out HObject ho_ConcatLine, out HTuple hv_RowBegin, out HTuple hv_ColBegin, out HTuple hv_RowEnd, out HTuple hv_ColEnd)
        {



            // Local iconic variables 

            HObject ho_Contour, ho_Contour1;

            // Local control variables 

            HTuple  hv_Rows = null, hv_Cols = null;
            hv_RowBegin = null;
            hv_ColBegin = null;
            hv_RowEnd = null;
            hv_ColEnd = null;
            HTuple hv_Nr = null, hv_Nc = null, hv_Dist = null,number = null;
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_ConcatLine);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            HOperatorSet.GenEmptyObj(out ho_Contour1);

            HOperatorSet.CountObj(ho_LineContours, out number);
            HTuple end_val = number,hv_Row,hv_Col;
            HTuple step_val = 1;
            HTuple hv_i = 1;
            for (hv_i = 1; hv_i.Continue(end_val, step_val); hv_i = hv_i.TupleAdd(step_val))
            {
                HObject ho_ObjectSelected;
                HOperatorSet.SelectObj(ho_LineContours, out ho_ObjectSelected, hv_i);
                HOperatorSet.GetContourXld(ho_ObjectSelected, out hv_Row, out hv_Col);
                HOperatorSet.TupleConcat(hv_Rows, hv_Row, out hv_Rows);
                HOperatorSet.TupleConcat(hv_Cols, hv_Col, out hv_Cols);
                ho_ObjectSelected.Dispose();
            }
            ho_Contour.Dispose();




           // HOperatorSet.GetContourXld(ho_LineContours, out hv_Row2, out hv_Col2);

            HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_Rows, hv_Cols);
            HOperatorSet.FitLineContourXld(ho_Contour, "tukey", -1, 0, 5, 2, out hv_RowBegin, out hv_ColBegin, out hv_RowEnd, out hv_ColEnd, out hv_Nr, out hv_Nc, out hv_Dist);
            ho_Contour1.Dispose();
            HOperatorSet.GenContourPolygonXld(out ho_Contour1, hv_RowBegin.TupleConcat(hv_RowEnd), hv_ColBegin.TupleConcat(hv_ColEnd));
            ho_Contour.Dispose();
            ho_Contour1.Dispose();

            return;
        }

        public static void C_Fit(out HObject ho_FitCircle, HTuple hv_Rows, HTuple hv_Cols, out HTuple hv_Row, out HTuple hv_Column, out HTuple hv_Radius)
        {



            // Local iconic variables 

            HObject ho_Contour;

            // Local control variables 

            hv_Row = null;
            hv_Column = null;
            hv_Radius = null;
            HTuple hv_StartPhi = null, hv_EndPhi = null, hv_PointOrder = null;
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_FitCircle);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            ho_Contour.Dispose();
            HOperatorSet.GenContourNurbsXld(out ho_Contour, hv_Rows, hv_Cols, "auto", "auto",  3, 1, 5);
            //segment_contours_xld (Contour, ContoursSplit, 'lines_circles', 5, 4, 2)
            HOperatorSet.FitCircleContourXld(ho_Contour, "algebraic", -1, 0, 0, 3, 2, out hv_Row, out hv_Column, out hv_Radius, out hv_StartPhi, out hv_EndPhi, out hv_PointOrder);
            ho_Contour.Dispose();

            return;
        }

        public static void CC_Mid(HObject ho_Circle1, HObject ho_Circle2, out HObject ho_MidCircle, out HTuple hv_centreRow, out HTuple hv_centreCol, out HTuple hv_midRadius)
        {



            // Local iconic variables 

            // Local control variables 

            HTuple hv_Row1 = null, hv_Column1 = null, hv_Radius1 = null;
            HTuple hv_StartPhi1 = null, hv_EndPhi1 = null, hv_PointOrder1 = null;
            HTuple hv_Row2 = null, hv_Column2 = null, hv_Radius2 = null;
            HTuple hv_StartPhi2 = null, hv_EndPhi2 = null, hv_PointOrder2 = null;
            hv_centreRow = null;
            hv_centreCol = null;
            hv_midRadius = null;
            HTuple hv_midStartPhi = null, hv_midEndPhi = null;
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_MidCircle);
            HOperatorSet.FitCircleContourXld(ho_Circle1, "algebraic", -1, 0, 0, 3, 2, out hv_Row1, out hv_Column1, out hv_Radius1, out hv_StartPhi1, out hv_EndPhi1, out hv_PointOrder1);
            HOperatorSet.FitCircleContourXld(ho_Circle2, "algebraic", -1, 0, 0, 3, 2, out hv_Row2,out hv_Column2, out hv_Radius2, out hv_StartPhi2, out hv_EndPhi2, out hv_PointOrder2);

            hv_centreRow = (0.5 * hv_Row1) + (0.5 * hv_Row2);
            hv_centreCol = (0.5 * hv_Column1) + (0.5 * hv_Column2);
            hv_midRadius = (0.5 * hv_Radius1) + (0.5 * hv_Radius2);
            hv_midStartPhi = (0.5 * hv_StartPhi1) + (0.5 * hv_StartPhi2);
            hv_midEndPhi = (0.5 * hv_EndPhi1) + (0.5 * hv_EndPhi2);
            ho_MidCircle.Dispose();
            HOperatorSet.GenCircleContourXld(out ho_MidCircle, hv_centreRow, hv_centreCol, hv_midRadius, hv_midStartPhi, hv_midEndPhi, "positive", 1);

            return;
        }

        public static void C_SUP(out HObject ho_Circle, HTuple hv_Row, HTuple hv_Column, HTuple hv_Radius)
        {


            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Circle);
            ho_Circle.Dispose();
            HOperatorSet.GenCircleContourXld(out ho_Circle, hv_Row, hv_Column, hv_Radius, 0, 6.28318, "positive", 1);

            return;
        }

        public static void LL_InnerC(HObject ho_LineObject, out HObject ho_Circle, out HTuple hv_Row, out HTuple hv_Column, out HTuple hv_Radius)
        {
            // Local iconic variables 
            HObject ho_ObjectSelected = null, ho_Region;

            // Local control variables 
            HTuple hv_Number = null, hv_Rows = null, hv_Cols = null;
            HTuple hv_i = null, hv_Col = new HTuple();
            hv_Row = null;
            hv_Column = null;
            hv_Radius = null;
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Circle);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.CountObj(ho_LineObject, out hv_Number);
            hv_Rows = new HTuple();
            hv_Cols = new HTuple();
            HTuple end_val3 = hv_Number;
            HTuple step_val3 = 1;
            for (hv_i = 1; hv_i.Continue(end_val3, step_val3); hv_i = hv_i.TupleAdd(step_val3))
            {
                ho_ObjectSelected.Dispose();
                HOperatorSet.SelectObj(ho_LineObject, out ho_ObjectSelected, hv_i);
                HOperatorSet.GetContourXld(ho_ObjectSelected, out hv_Row, out hv_Col);
                HOperatorSet.TupleConcat(hv_Rows, hv_Row, out hv_Rows);
                HOperatorSet.TupleConcat(hv_Cols, hv_Col, out hv_Cols);
            }


            ho_Region.Dispose();
            HOperatorSet.GenRegionPolygonFilled(out ho_Region, hv_Rows, hv_Cols);


            HOperatorSet.InnerCircle(ho_Region, out hv_Row, out hv_Column, out hv_Radius);
            ho_Circle.Dispose();
            HOperatorSet.GenCircleContourXld(out ho_Circle, hv_Row, hv_Column, hv_Radius, 0, 6.28318, "positive", 1);
            ho_ObjectSelected.Dispose();
            ho_Region.Dispose();

            return;
        }

        public static void Arc_Concat(HObject ho_ArcTuple, out HObject ho_Circle, out HTuple hv_Row1, out HTuple hv_Column, out HTuple hv_Radius)
        {



            // Local iconic variables 

            HObject ho_ObjectSelected = null, ho_Contour;

            // Local control variables 

            HTuple hv_Number = null, hv_Rows = null, hv_Cols = null;
            HTuple hv_i = null, hv_Row = new HTuple(), hv_Col = new HTuple();
            hv_Row1 = null;
            hv_Column = null;
            hv_Radius = null;
            HTuple hv_StartPhi = null, hv_EndPhi = null, hv_PointOrder = null;
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Circle);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            HOperatorSet.CountObj(ho_ArcTuple, out hv_Number);
            hv_Rows = new HTuple();
            hv_Cols = new HTuple();
            HTuple end_val3 = hv_Number;
            HTuple step_val3 = 1;
            for (hv_i = 1; hv_i.Continue(end_val3, step_val3); hv_i = hv_i.TupleAdd(step_val3))
            {
                ho_ObjectSelected.Dispose();
                HOperatorSet.SelectObj(ho_ArcTuple, out ho_ObjectSelected, hv_i);
                HOperatorSet.GetContourXld(ho_ObjectSelected, out hv_Row, out hv_Col);
                HOperatorSet.TupleConcat(hv_Rows, hv_Row, out hv_Rows);
                HOperatorSet.TupleConcat(hv_Cols, hv_Col, out hv_Cols);
            }
            ho_Contour.Dispose();
            HOperatorSet.GenContourNurbsXld(out ho_Contour, hv_Rows, hv_Cols, "auto", "auto", 3, 1, 5);
            HOperatorSet.FitCircleContourXld(ho_Circle, "algebraic", -1, 0, 0, 3, 2, out hv_Row1, out hv_Column, out hv_Radius, out hv_StartPhi, out hv_EndPhi, out hv_PointOrder);
            ho_ObjectSelected.Dispose();
            ho_Contour.Dispose();

            return;
        }


        public static void LL_Dist(HObject ho_Line1Contours, HObject ho_Line2Contours, out HTuple hv_Distance)
        {
            // Local iconic variables 

            // Local control variables 

            HTuple  hv_RowBegin1 = null, hv_ColBegin1 = null;
            HTuple hv_RowEnd1 = null, hv_ColEnd1 = null, hv_Nr = null;
            HTuple hv_Nc = null, hv_Dist = null, hv_RowBegin2 = null;
            HTuple hv_ColBegin2 = null, hv_RowEnd2 = null, hv_ColEnd2 = null;
            HTuple hv_theta1 = null, hv_theta2 = null, hv_MidRow = null;
            HTuple hv_MidCol = null, hv_MidColEnd = null, hv_MidRowEnd = null;
            HTuple hv_MidColBegin = null, hv_MidRowBegin = null;
            // Initialize local and output iconic variables 
            hv_Distance = new HTuple();

            
            HOperatorSet.FitLineContourXld(ho_Line1Contours, "tukey", 20, 0, 10, 2, out hv_RowBegin1,
                out hv_ColBegin1, out hv_RowEnd1, out hv_ColEnd1, out hv_Nr, out hv_Nc, out hv_Dist);
            HOperatorSet.FitLineContourXld(ho_Line2Contours, "tukey", 20, 0, 10, 2, out hv_RowBegin2,
                out hv_ColBegin2, out hv_RowEnd2, out hv_ColEnd2, out hv_Nr, out hv_Nc, out hv_Dist);
            HOperatorSet.LineOrientation(hv_RowBegin1, hv_ColBegin1, hv_RowEnd1, hv_ColEnd1,
                out hv_theta1);
            HOperatorSet.LineOrientation(hv_RowBegin2, hv_ColBegin2, hv_RowEnd2, hv_ColEnd2,
                out hv_theta2);

            if ((int)(new HTuple(hv_theta1.TupleLess(0))) != 0)
            {
                hv_theta1 = hv_theta1 + Pi;
            }
            if ((int)(new HTuple(hv_theta2.TupleLess(0))) != 0)
            {
                hv_theta2 = hv_theta2 + Pi;
            }
            hv_MidRow = 0;
            hv_MidCol = 0;
            hv_MidColEnd = 0;
            hv_MidRowEnd = 0;
            hv_MidColBegin = 0;
            hv_MidRowBegin = 0;

            if ((int)(new HTuple(((((hv_theta1 - hv_theta2)).TupleAbs())).TupleLessEqual(0.5))) != 0)
            {
                //*认为是平行的
                HOperatorSet.DistancePl(hv_RowBegin1, hv_ColBegin1, hv_RowBegin2, hv_ColBegin2,
                    hv_RowEnd2, hv_ColEnd2, out hv_Distance);
            }

            return;
        }

        public static void Arc_Angle(HObject arc_LineContours,out HTuple angle)
        {
            HTuple row, column, radius, startPhi, endPhi, pointOrder;
            HOperatorSet.FitCircleContourXld(arc_LineContours, "algebraic", -1, 0.0, 0, 3, 2.0, out row, out column, out radius, out startPhi, out endPhi, out pointOrder);
            angle = 2 * Pi - endPhi - startPhi;
        }


    }
}
